<?php
	define( "HOST" , 'localhost' );
	define( "DBNAME" , "scutter_f" );
	define( "DBPASS" , "rwhe08" );
	define( "DATABASE" , "scutter" );

	class database {
		private $host;
		private $user;
		private $password;
		private $dbname;
		private $connection;
		
		function __construct($host = HOST ,$user = DBNAME ,$password = DBPASS ,$dbname = DATABASE){
			$this->host = $host;
			$this->user = $user;
			$this->password = $password;
			$this->dbname = $dbname;
		}
		
		function exec_sql($query,$feedback = True){
			try{
				@ $this->connection = mysql_connect($this->host,$this->user,$this->password) or die('无法连接数据库！' );
				mysql_select_db($this->dbname);
				$result = mysql_query($query);
			}
			catch(Exception $e){
				return false;
			}
			if(!$feedback) return True;
			while($row = mysql_fetch_array($result)){
				if($row) $info[] = $row;
			}
			return $info;
		}
		
		function is_sinaid_exist($jwuser){
			$query = "SELECT `id` FROM `web_jw2005` WHERE `jw_user`='$jwuser'";
			$result = $this->exec_sql($query);
			$is_sinaid_exist = $result[0];
			return $is_sinaid_exist;
		}
		
		function regist($sinaid,$access_token){
			if($this->is_sinaid_exist($sinaid)){
				$query = "UPDATE `web_jw2005` SET `access_token`='$access_token' WHERE `sina_id`='$sinaid'";
			}else{
				$query = "INSERT INTO `web_jw2005` (`sina_id`,`access_token`,`reg_time`,`jw_user`,`jw_pass`,`is_pk`,`is_cron`,`is_new`) VALUES ('$sinaid','$access_token',NOW(),'','','0','0','1')";			
			}	
			return $this->exec_sql($query,false);
		}
		
		function addscanning($jwuser,$jwpass,$email){
			if($this->is_sinaid_exist($jwuser)){
				$query = "UPDATE `web_jw2005` SET `email`='$email',`is_cron`='1' WHERE `jw_user`='$jwuser'";
			}else{
				$query = "INSERT INTO `web_jw2005` (`jw_user`,`jw_pass`,`email`,`reg_time`,`is_cron`) VALUES ('$jwuser','$jwpass','$email',NOW(),'1')";			
			}	
			return $this->exec_sql($query,false);
		}
		
		function addevaluation($jwuser,$jwpass,$email){
			if($this->is_sinaid_exist($jwuser)){
				$query = "UPDATE `web_jw2005` SET `email`='$email',`is_pk`='1' WHERE `jw_user`='$jwuser'";
			}else{
				$query = "INSERT INTO `web_jw2005` (`jw_user`,`jw_pass`,`email`,`reg_time`,`is_pk`) VALUES ('$jwuser','$jwpass','$email',NOW(),'1')";				}	
			return $this->exec_sql($query,false);
		}
	}
	
	class sinadb {
		private $host;
		private $user;
		private $password;
		private $dbname;
		private $connection;
		
		function __construct($host = HOST ,$user = DBNAME ,$password = DBPASS ,$dbname = DATABASE){
			$this->host = $host;
			$this->user = $user;
			$this->password = $password;
			$this->dbname = $dbname;
		}
		
		function exec_sql($query,$feedback = True){
			try{
				@ $this->connection = mysql_connect($this->host,$this->user,$this->password) or die('鏃犳硶杩炴帴鏁版嵁搴擄紒' );
				mysql_select_db($this->dbname);
				$result = mysql_query($query);
			}
			catch(Exception $e){
				return false;
			}
			if(!$feedback) return True;
			while($row = mysql_fetch_array($result)){
				if($row) $info[] = $row;
			}
			return $info;
		}
		
		function is_sinaid_exist($sinaid){
			$query = "SELECT `id` FROM `sina_jw2005` WHERE `sina_id`='$sinaid'";
			$result = $this->exec_sql($query);
			$is_sinaid_exist = $result[0];
			return $is_sinaid_exist;
		}
		
		function regist($sinaid,$access_token){
			if($this->is_sinaid_exist($sinaid)){
				$query = "UPDATE `sina_jw2005` SET `access_token`='$access_token' WHERE `sina_id`='$sinaid'";
			}else{
				$query = "INSERT INTO `sina_jw2005` (`sina_id`,`access_token`,`reg_time`,`jw_user`,`jw_pass`,`is_pk`,`is_cron`,`is_new`) VALUES ('$sinaid','$access_token',NOW(),'','','0','0','1')";			
			}	
			return $this->exec_sql($query,false);
		}
		
		function addscanning($sinaid,$jwuser,$jwpass,$email){
			$query = "UPDATE `sina_jw2005` SET `is_cron`='1' , `jw_user`='$jwuser' , `jw_pass`='$jwpass' , `email`='$email' WHERE `sina_jw2005`.`sina_id`='$sinaid'";
			return $this->exec_sql($query,false);
		}
		
		function addevaluation($sinaid,$jwuser,$jwpass,$email){
			$query = "UPDATE `sina_jw2005` SET `is_pk`='1' , `jw_user`='$jwuser' , `jw_pass`='$jwpass' , `email`='$email' WHERE `sina_jw2005`.`sina_id`='$sinaid'";
			return $this->exec_sql($query,false);
		}
	}
	//$db = new database();
	//$result = $db->regist("2008063675");
	//echo "<pre>";if($result)echo "1";else echo "0";//print_r($result);
?>